package com.xpsoft.oa.service.project.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.core.util.ContextUtil;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.oa.dao.project.ProjectFinalauditDao;
import com.xpsoft.oa.dao.project.ProjectFinalauditInfoDao;
import com.xpsoft.oa.model.project.ProjectFinalaudit;
import com.xpsoft.oa.model.project.ProjectFinalauditInfo;
import com.xpsoft.oa.service.project.ProjectFinalauditService;

public class ProjectFinalauditServiceImpl extends BaseServiceImpl<ProjectFinalaudit> implements ProjectFinalauditService{
	private ProjectFinalauditDao dao;
	@Autowired
	private ProjectFinalauditInfoDao projectFinalauditInfoDao;
	public ProjectFinalauditServiceImpl(ProjectFinalauditDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveProjectFinalaudit(ProjectFinalaudit projectFinalaudit,
			String dataList) {
		// TODO Auto-generated method stub
		Boolean isNew = true;//新增标志
		if(projectFinalaudit.getId()!=null){
			isNew = false;
		}else{
			projectFinalaudit.setCreateTime(new Date());
			projectFinalaudit.setCreateUser(ContextUtil.getCurrentUser());
		}
		dao.save(projectFinalaudit);
		if(StringUtils.isNotBlank(dataList)){
			List<ProjectFinalauditInfo> projectFinalauditInfoList = JsonUtil.fromJsonTypes(dataList,
					new TypeToken<List<ProjectFinalauditInfo>>() {
					}.getType());
			if(!isNew){//修改
				List<Long> idList = new ArrayList<Long>();
				for(ProjectFinalauditInfo projectFinalauditInfo:projectFinalauditInfoList){
					if(projectFinalauditInfo.getId()!=null){//修改的记录
						idList.add(projectFinalauditInfo.getId());
					}
				}
				if(idList.size()>0){//先进行删除
					projectFinalauditInfoDao.deleteProjectFinalauditInfos(projectFinalaudit.getId(), idList);
				}
			}
			for(ProjectFinalauditInfo projectFinalauditInfo:projectFinalauditInfoList){
				projectFinalauditInfo.setFinalAudit(projectFinalaudit);
				projectFinalauditInfoDao.save(projectFinalauditInfo);
			}
		}
	}

}